<?php
$ArrBulan = Array(
    1 => "Januari",
    2 => "Februari",
    3 => "Maret",
    4 => "April",
    5 => "Mei",
    5 => "Mei",
    6 => "Juni",
    7 => "Juli",
    8 => "Agustus",
    9 => "September",
    10 => "Oktober",
    11 => "November",
    12 => "Desember");

# function print an array variable

function printArray($arr) {
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}

# set page title

function setPageTitle($title) {
    global $__CFG_page_title;

    $__CFG_page_title = $title;
}

# timer function

function get_timer() {
    list($usec, $sec) = explode(" ", microtime());

    return ((float) $usec + (float) $sec);
}

#creating combo dropdown

function createCombo($sql = "", $fieldval = "", $fieldstring = "", $nama = "", $kode = "", $addOption = "", $addAction = "") {
    global $dbi;

    $row = $dbi->get_results($sql);
    $hasil = "<select name=\"" . $nama . "\" id=\"" . $nama . "\" " . $addAction . ">";
    if ($addOption != "")
        $hasil .= $addOption;
    if (sizeof($row) > 0) {
        foreach ($row as $line) {
            $hasil .= "<option value=\"" . $line->$fieldval . "\"" . ($line->$fieldval == $kode ? " selected" : "") . ">" . $line->$fieldstring . "</option>";
        }
    }
    $hasil .= "</select>";

    return $hasil;
}

#get name of data master

function getNamaMaster($kode, $t_master) {
    global $dbi;

    $sql = "select " . $t_master . " as nama from " . $t_master . " where kode = '" . $kode . "'";
    $row = $dbi->get_row($sql);

    return $row->nama;
}

function getNama($kode, $t_name, $t_field, $whereField = "", $opr = "", $order = "", $limit = "") {
    global $dbi;
    $whereField = $whereField == "" ? "kode" : $whereField;
    if ($opr != "") {
        $sql = "select " . $t_field . " as nama from " . $t_name . " where " . $whereField . " like('%" . $kode . "%')" .
                ($order != "" ? " ORDER BY " . $order : "") . ($limit != "" ? " limit " . $limit : "");
    } else {
        $sql = "select " . $t_field . " as nama from " . $t_name . " where " . $whereField . " = '" . $kode . "'" .
                ($order != "" ? " ORDER BY " . $order : "") . ($limit != "" ? " limit " . $limit : "");
    }
    //echo $sql;
    $row = $dbi->get_row($sql);

    return $row->nama;
}

function getScalar($sql, $fieldselected = "f") {
    global $dbi;
    return $dbi->get_row($sql)->$fieldselected;
}

#get date last "n" days

function getlastdays($currdate, $numdays) {

    $date_array = getdate(strtotime($currdate));
    $time_date = $date_array[0];

    $returndate = date("Y-m-d", $time_date - ($numdays * 24 * 60 * 60));

    return $returndate;
}

//format tanggal inputan dd-mm-yyyy
function formatDate2DB($tgl, $iswaktu = false) {
    $arrTgl = explode(" ", $tgl);
    $tgle = $arrTgl[0];
    $wkte = $arrTgl[1];

    list($tanggal, $bulan, $tahun) = explode("-", $tgle);

    if ($tgl != "")
        return $tahun . "-" . $bulan . "-" . $tanggal . ($iswaktu ? $wkte : "");
    else
        return "";
}

//format tanggal inputan yyyy-mm-dd
function formatDB2Date($tgl, $iswaktu = false, $isbahasa = false) {
    global $ArrBulan;
    $arrTgl = explode(" ", $tgl);
    $tgle = $arrTgl[0];
    $wkte = $arrTgl[1];

    list($tahun, $bulan, $tanggal) = explode("-", $tgle);

    if ($tgl != "") {
        if (!$isbahasa)
            return $tanggal . "-" . $bulan . "-" . $tahun . ($iswaktu ? $wkte : "");
        else
            return $tanggal . " " . $ArrBulan[intval($bulan)] . " " . $tahun;
    }
    else
        return "";
}

function stringToArrayObject($str) {
    $arr1 = explode("<;>", $str);
    $arrCols = explode("<#>", $arr1[0]);
    for ($i = 1; $i < sizeof($arr1); $i++) {
        $arrBaris = explode("<#>", $arr1[$i]);
        $x = 0;
        $y = $i - 1;
        foreach ($arrCols as $cols) {
            $arrData[$y]->$cols = $arrBaris[$x];
            $x++;
        }
    }

    return $arrData;
}

function AddOptions($start, $end, $value) {
    for ($i = $start; $i <= $end; $i++) {
        ?>
        <option value="<?php echo $i ?>"<?php echo ($i == $value ? " selected" : "") ?>><?php echo $i ?></option>
        <?php
    }
}

function JSTextMask($arrElm, $arrMask) {
    ?>
    <script src="<?php echo $__CFG_http_js; ?>T_JQuery.min.js"></script>
    <script src="<?php echo $__CFG_http_js ?>jquery.maskedinput-1.2.2.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(document).ready(function()
        {
    <?php
    if (sizeof($arrElm) > 0 && sizeof($arrMask)) {
        for ($x = 0; $x < sizeof($arrElm); $x++) {
            ?>
                    $("#<?php echo $arrElm[x] ?>").mask("<?php echo $arrMask[x] ?>");
            <?php
        }
    }
    ?>
        });
    </script>
    <?php
}

function GiveAttributeNot($elm, $value, $attr) {
    $hasil = "";
    if ($elm != $value) {
        $hasil = $attr;
    }

    return $hasil;
}

function GiveAttribute($elm, $value, $attr) {
    $hasil = "";
    if ($elm == $value) {
        $hasil = $attr;
    }

    return $hasil;
}

function dateFormat($date, $format = "") {
    global $ArrBulan;

    $dts = explode(" ", $date);
    $d = explode("-", $dts[0]);
    $t = explode(":", $dts[1]);
    $bln = (int) $d[1];

    return str_pad($d[2], 2, "0", 0) . " " . $ArrBulan[$bln] . " " . $d[0];
}

function sortingField($title, $sf, $sf_curr, $sd_curr) {
    $cfs = $_GET["fs"];
    $cfv = $_GET["fv"];
    $addLink = "";
    if ($cfs != "" && $cfv != "")
        $addLink = "&fs=" . $cfs . "&fv=" . $cfv;
    $sd = "DESC";
    if ($sf == $sf_curr && $sd == $sd_curr)
        $sd = "ASC";
    $link = "<a class=\"sorting " . strtolower($sd) . "\" href=\"?csf=" . $sf . "&csd=" . $sd . $addLink . "\" title=\"Sorting " . $title . " " . $sd . "\">" . $title . "</a>";

    return $link;
}

function getTitleInfo($kode_staff = "") {
    global $auth;
    if ($kode_staff == "")
        $kode_staff = $auth->getUserID();
    $nama = getNama($kode_staff, "pegawai", "nama", "userid");
    $nrp = getNama($kode_staff, "pegawai", "nrp", "userid");

    return $nama . "/" . ($nrp == "" ? "-" : $nrp);
}

function generateStaffMenuItem($c_page = "edit", $showchilditem = true, $disabledchilditem = true, $staff_type = "") {
    global $__CFG_http_adminroot;

    $kode_staff = $_GET["idk"];
    $mode = "staff";
    switch ($staff_type) {
        case "_detail":
            $type_form_staff = "index";
            break;
        case "_input":
            $type_form_staff = "staff_family";
            break;
        case "mymenu":
            $type_form_staff = "profil";
            $mode = "";
            break;
        default:
            $type_form_staff = "edit";
            $mymenu = "staff";
            break;
    }

    $link = $__CFG_http_adminroot . "index.php/" . $mymenu . $staff_type . "/";
    $menu = "<div id=\"menu_item\"><ul class=\"staff_menu\">";
    if ($staff_type != "_input") {
        $menu .= "<li" . ($c_page == "edit" || $c_page == "profil" ? " class=\"active\"" : "") . ">" . ($c_page != "edit" ? "<a href=\"" . $link . $type_form_staff . "/?idk=" . $kode_staff . "\">" : "") . "Data Karyawan" . ($c_page != "edit" ? "</a>" : "") . "</li>";
    }
    $menu .= "<li" . ($c_page == "staff_family" || $c_page == "staff_family_edit" ? " class=\"active\"" : "") . ">" . ($disabledchilditem ? "<a href=\"" . $link . "" . "staff_family/?idk=" . $kode_staff . "\">" : "") . "Data Keluarga" . ($disabledchilditem ? "</a>" : "") . "</li>";
    //$menu .= "<li".($c_page=="staff_emergency" || $c_page=="staff_emergency_edit"?" class=\"active\"":"").">".($disabledchilditem?"<a href=\"".$link.""."staff_emergency/?idk=". $kode_staff ."\">":"")."Data Keluarga Yang Dihubungi".($disabledchilditem?"</a>":"")."</li>";
    $menu .= "<li" . ($c_page == "kualifikasi_rekap" ? " class=\"active\"" : "") . ">" . ($disabledchilditem ? "<a href=\"" . $link . "" . "kualifikasi_rekap/?idk=" . $kode_staff . "\">" : "") . "Riwayat Kualifikasi" . ($disabledchilditem ? "</a>" : "") . "</li>";
    $menu .= "<li" . ($c_page == "staff_education" || $c_page == "staff_education_edit" ? " class=\"active\"" : "") . ">" . ($disabledchilditem ? "<a href=\"" . $link . "" . "staff_education/?idk=" . $kode_staff . "\">" : "") . "Data Riwayat Pendidikan" . ($disabledchilditem ? "</a>" : "") . "</li>";
    $menu .= "<li" . ($c_page == "riwayat_pelatihan" || $c_page == "riwayat_pelatihan_edit" ? " class=\"active\"" : "") . ">" . ($disabledchilditem ? "<a href=\"" . $link . "" . "riwayat_pelatihan/?idk=" . $kode_staff . "\">" : "") . "Data Riwayat Pelatihan" . ($disabledchilditem ? "</a>" : "") . "</li>";
    $menu .= "<li" . ($c_page == "staff_status" || $c_page == "staff_status_edit" ? " class=\"active\"" : "") . ">" . ($disabledchilditem ? "<a href=\"" . $link . "" . "staff_status/?idk=" . $kode_staff . "\">" : "") . "Sejarah Karir Internal" . ($disabledchilditem ? "</a>" : "") . "</li>";
    $menu .= "<li" . ($c_page == "staff_status_external" || $c_page == "staff_status_external_edit" ? " class=\"active\"" : "") . ">" . ($disabledchilditem ? "<a href=\"" . $link . "" . "staff_status_external/?idk=" . $kode_staff . "\">" : "") . "Sejarah Karir Eksternal" . ($disabledchilditem ? "</a>" : "") . "</li>";
    /* $menu .= "<li".($c_page=="staff_sk" || $c_page=="staff_sk_edit"?" class=\"active\"":"").">".($disabledchilditem?"<a href=\"".$link.""."staff_sk/?idk=". $kode_staff ."\">":"")."Data SK Karyawan".($disabledchilditem?"</a>":"")."</li>";
      $menu .= "<li".($c_page=="staff_prestasi" || $c_page=="staff_prestasi_edit"?" class=\"active\"":"").">".($disabledchilditem?"<a href=\"".$link.""."staff_prestasi/?idk=". $kode_staff ."\">":"")."Data Prestasi Karyawan".($disabledchilditem?"</a>":"")."</li>";
      $menu .= "<li".($c_page=="staff_seminar" || $c_page=="staff_seminar_edit"?" class=\"active\"":"").">".($disabledchilditem?"<a href=\"".$link.""."staff_seminar/?idk=". $kode_staff ."\">":"")."Data Seminar Karyawan".($disabledchilditem?"</a>":"")."</li>";
      $menu .= "<li".($c_page=="staff_status" || $c_page=="staff_status_edit"?" class=\"active\"":"").">".($disabledchilditem?"<a href=\"".$link.""."staff_status/?idk=". $kode_staff ."\">":"")."Data Sejarah Karir Karyawan".($disabledchilditem?"</a>":"")."</li>";
      $menu .= "<li".($c_page=="staff_kompetensi" || $c_page=="staff_kompetensi_edit"?" class=\"active\"":"").">".($disabledchilditem?"<a href=\"".$link.""."staff_kompetensi/?idk=". $kode_staff ."\">":"")."Data Kompetensi Karyawan".($disabledchilditem?"</a>":"")."</li>";
      $menu .= "<li".($c_page=="staff_tugas_mengajar" || $c_page=="staff_tugas_mengajar_edit"?" class=\"active\"":"").">".($disabledchilditem?"<a href=\"".$link.""."staff_tugas_mengajar/?idk=". $kode_staff ."\">":"")."Data Tugas".($disabledchilditem?"</a>":"")."</li>";
      $menu .= "<li".($c_page=="staff_tugas_tambahan" || $c_page=="staff_tugas_tambahan_edit"?" class=\"active\"":"").">".($disabledchilditem?"<a href=\"".$link.""."staff_tugas_tambahan/?idk=". $kode_staff ."\">":"")."Data Tugas Tambahan".($disabledchilditem?"</a>":"")."</li>";
      $menu .= "<li".($c_page=="tr_medical" || $c_page=="tr_medical_edit"?" class=\"active\"":"").">".($disabledchilditem?"<a href=\"".$link.""."tr_medical/?idk=". $kode_staff ."\">":"")."Catatan Medis Karyawan".($disabledchilditem?"</a>":"")."</li>";
      if($staff_type!="") {
      $menu .= "<li".($c_page=="tr_absensi" || $c_page=="tr_absensi_edit"?" class=\"active\"":"").">".($disabledchilditem?"<a href=\"".$link.""."tr_absensi/?idk=". $kode_staff ."\">":"")."Transaksi Absensi Karyawan".($disabledchilditem?"</a>":"")."</li>";
      $menu .= "<li".($c_page=="tr_cuti" || $c_page=="tr_cuti_edit"?" class=\"active\"":"").">".($disabledchilditem?"<a href=\"".$link.""."tr_cuti/?idk=". $kode_staff ."\">":"")."Transaksi Cuti Karyawan".($disabledchilditem?"</a>":"")."</li>";
      }
      switch($staff_type) {
      case "_detail":
      $link = $link = $__CFG_http_adminroot."index.php/querydata/staff/";
      break;
      default:
      $link = $__CFG_http_adminroot."index.php/staff".$staff_type."/index/";
      break;
      } */
    if ($staff_type != "mymenu") {
        $menu .= "<li class=\"kembali\"><a href=\"" . $link . "\">Kembali ke daftar karyawan</a></li>";
    }
    $menu .= "</ul></div>";

    echo $menu;
}

function GenerateNomorUsulan($max, $nomor) {
    return $nomor . substr(date("Y"), 2) . str_pad(($max + 1), 4, "0", 0);
}

function dateFormatIndo($tgl) {
    return "";
}
?>
